import React, { useEffect, useState } from "react";
import {
  HashRouter as Router,
  Route,
  Routes,
  Navigate,
} from "react-router-dom";
import Login from "../views/login";
import NewsSandBox from "../views/sandBox/NewsSandBox";
import Home from "../views/sandBox/home/Home";
import UserList from "../views/sandBox/user-manage/UserList";
import RoleList from "../views/sandBox/right-manage/RoleList";
import RightList from "../views/sandBox/right-manage/RightList";
import NoPermission from "../views/sandBox/error-page/NoPermission";
import NewsAdd from "../views/sandBox/news-manage/NewsAdd";
import NewsDraft from "../views/sandBox/news-manage/NewsDraft";
import NewsCategory from "../views/sandBox/news-manage/NewsCategory";
import Audit from "../views/sandBox/audit-manage/Audit";
import AuditList from "../views/sandBox/audit-manage/AuditList";
import Unpublished from "../views/sandBox/publish-manage/Unpublished";
import Published from "../views/sandBox/publish-manage/Published";
import Sunset from "../views/sandBox/publish-manage/Sunset";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import axios from "axios";

const LocalRoute = {
  "/home": <Home />,
  "/user-manage/list": <UserList />,
  "/right-manage/right/list": <RightList />,
  "/right-manage/role/list": <RoleList />,
  "/news-manage/add": <NewsAdd />,
  "/news-manage/draft": <NewsDraft />,
  "/news-manage/category": <NewsCategory />,
  "/audit-manage/audit": <Audit />,
  "/audit-manage/list": <AuditList />,
  "/publish-manage/unpublished": <Unpublished />,
  "/publish-manage/published": <Published />,
  "/publish-manage/sunset": <Sunset />,
};
const Index = () => {
  NProgress.start();
  const [BackRouteList, setBackRouteList] = useState([]);
  const baseUrl = "";
  useEffect(() => {
    Promise.all([
      axios.get(baseUrl + "/rights"),
      axios.get(baseUrl + "/children"),
    ]).then((res) => {
      setBackRouteList([...res[0].data, ...res[1].data]);
    });
  }, [baseUrl]);

  const {
    role: { rights },
  } = JSON.parse(localStorage.getItem("token"));
  function checkRoute(item) {
    return LocalRoute[item.key] && item.pagepermisson == 1;
  }

  function checkUserPermission(item) {
    return rights.includes(item.key);
  }

  return (
    <Router>
      <Routes>
        <Route path="/login" element={<Login />}></Route>
        {/*<Route path='/' element={<NewsSandBox />}></Route>*/}
        <Route
          path="/"
          element={
            localStorage.getItem("token") ? (
              <NewsSandBox />
            ) : (
              <Navigate to="/login" />
            )
          }
        >
          <Route path="/" element={<Navigate to="/home" />}></Route>
          {BackRouteList.map((item) => {
            if (checkRoute(item) && checkUserPermission(item)) {
              return (
                <Route
                  path={item.key}
                  element={LocalRoute[item.key]}
                  key={item.key}
                ></Route>
              );
            } else {
              return (
                <Route
                  path="*"
                  element={<NoPermission />}
                  key={item.key}
                ></Route>
              );
            }
          })}

          {BackRouteList.length > 0 && (
            <Route path="*" element={<NoPermission />}></Route>
          )}
        </Route>
      </Routes>
    </Router>
  );
};

export default Index;
